//
// Created by HP on 2024/5/8.
//

#include "aurora_info.h"

#include "rtthread.h"
Aurora_Info::Aurora_Info(LCD_Driver *lcd_s,Aurora_Font *font_s){
    _lcd = lcd_s;
    _font = font_s;

    Lmode = 0;
    Rmode = 0;
}
Aurora_Info::~Aurora_Info(){
    ui2foc_msg.type = disable_detail;
    ui2foc_msg.cmd = IS_CMD;
}
void Aurora_Info::Init(){
    title_label.set_font_driver(_font);
    title_label.set_lcd_driver(_lcd);
    title_label.set_font_size(Aurora_Font::font_size_big);
    title_label.set_text("详细信息");
    title_label.set_pos(100,0);
    title_label.show();

    leftM_label.set_font_driver(_font);
    leftM_label.set_lcd_driver(_lcd);
    leftM_label.set_font_size(Aurora_Font::font_size_middle);
    leftM_label.set_text("左电机");
    leftM_label.set_pos(25,30);
    leftM_label.show();


    rightM_label.set_font_driver(_font);
    rightM_label.set_lcd_driver(_lcd);
    rightM_label.set_font_size(Aurora_Font::font_size_middle);
    rightM_label.set_text("右电机");
    rightM_label.set_pos(220,30);
    rightM_label.show();

    Lmode_label.set_font_driver(_font);
    Lmode_label.set_lcd_driver(_lcd);
    Lmode_label.set_font_size(Aurora_Font::font_size_small);
    Lmode_label.set_text("模式");
    Lmode_label.set_pos(2,65);
 //   Lmode_label.show();


    Rmode_label.set_font_driver(_font);
    Rmode_label.set_lcd_driver(_lcd);
    Rmode_label.set_font_size(Aurora_Font::font_size_small);
    Rmode_label.set_text("模式");
    Rmode_label.set_pos(165,65);
  //  Rmode_label.show();

    LUq_label.set_font_driver(_font);
    LUq_label.set_lcd_driver(_lcd);
    LUq_label.set_font_size(Aurora_Font::font_size_small);
    LUq_label.set_text("Uq  ");
    LUq_label.set_pos(2,85);
    LUq_label.show();


    RUq_label.set_font_driver(_font);
    RUq_label.set_lcd_driver(_lcd);
    RUq_label.set_font_size(Aurora_Font::font_size_small);
    RUq_label.set_text("Uq  ");
    RUq_label.set_pos(165,85);
    RUq_label.show();

    LCurrent_label.set_font_driver(_font);
    LCurrent_label.set_lcd_driver(_lcd);
    LCurrent_label.set_font_size(Aurora_Font::font_size_small);
    LCurrent_label.set_text("电流");
    LCurrent_label.set_pos(2,105);
    LCurrent_label.show();


    RCurrent_label.set_font_driver(_font);
    RCurrent_label.set_lcd_driver(_lcd);
    RCurrent_label.set_font_size(Aurora_Font::font_size_small);
    RCurrent_label.set_text("电流");
    RCurrent_label.set_pos(165,105);
    RCurrent_label.show();

    LSpeed_label.set_font_driver(_font);
    LSpeed_label.set_lcd_driver(_lcd);
    LSpeed_label.set_font_size(Aurora_Font::font_size_small);
    LSpeed_label.set_text("速度");
    LSpeed_label.set_pos(2,125);
    LSpeed_label.show();


    RSpeed_label.set_font_driver(_font);
    RSpeed_label.set_lcd_driver(_lcd);
    RSpeed_label.set_font_size(Aurora_Font::font_size_small);
    RSpeed_label.set_text("速度");
    RSpeed_label.set_pos(165,125);
    RSpeed_label.show();


    LPostion_label.set_font_driver(_font);
    LPostion_label.set_lcd_driver(_lcd);
    LPostion_label.set_font_size(Aurora_Font::font_size_small);
    LPostion_label.set_text("位置");
    LPostion_label.set_pos(2,145);
    LPostion_label.show();

    RPostion_label.set_font_driver(_font);
    RPostion_label.set_lcd_driver(_lcd);
    RPostion_label.set_font_size(Aurora_Font::font_size_small);
    RPostion_label.set_text("位置");
    RPostion_label.set_pos(165,145);
    RPostion_label.show();



    LArrow_label.set_font_driver(_font);
    LArrow_label.set_lcd_driver(_lcd);
    LArrow_label.set_font_size(Aurora_Font::font_size_small);
    LArrow_label.set_text(">");
    LArrow_label.set_pos(95,145);
    LArrow_label.show();

    RArrow_label.set_font_driver(_font);
    RArrow_label.set_lcd_driver(_lcd);
    RArrow_label.set_font_size(Aurora_Font::font_size_small);
    RArrow_label.set_text(">");
    RArrow_label.set_pos(260,145);
    RArrow_label.show();

    LmodeValue_label.set_font_driver(_font);
    LmodeValue_label.set_lcd_driver(_lcd);
    LmodeValue_label.set_font_size(Aurora_Font::font_size_small);
    LmodeValue_label.set_text("暂    停");
    LmodeValue_label.set_pos(70,65);
    //LmodeValue_label.show();

    RmodeValue_label.set_font_driver(_font);
    RmodeValue_label.set_lcd_driver(_lcd);
    RmodeValue_label.set_font_size(Aurora_Font::font_size_small);
    RmodeValue_label.set_text("暂    停");
    RmodeValue_label.set_pos(225,65);
   // RmodeValue_label.show();

    LUqValue_label.set_font_driver(_font);
    LUqValue_label.set_lcd_driver(_lcd);
    LUqValue_label.set_font_size(Aurora_Font::font_size_small);
    LUqValue_label.set_text("0.00");
    LUqValue_label.set_pos(42,85);
    LUqValue_label.show();

    RUqValue_label.set_font_driver(_font);
    RUqValue_label.set_lcd_driver(_lcd);
    RUqValue_label.set_font_size(Aurora_Font::font_size_small);
    RUqValue_label.set_text("0.00");
    RUqValue_label.set_pos(208,85);
    RUqValue_label.show();


    LCurrentValue_label.set_font_driver(_font);
    LCurrentValue_label.set_lcd_driver(_lcd);
    LCurrentValue_label.set_font_size(Aurora_Font::font_size_small);
    LCurrentValue_label.set_text("0.00");
    LCurrentValue_label.set_pos(42,105);
    LCurrentValue_label.show();

    RCurrentValue_label.set_font_driver(_font);
    RCurrentValue_label.set_lcd_driver(_lcd);
    RCurrentValue_label.set_font_size(Aurora_Font::font_size_small);
    RCurrentValue_label.set_text("0.00");
    RCurrentValue_label.set_pos(208,105);
    RCurrentValue_label.show();

    LSpeedValue_label.set_font_driver(_font);
    LSpeedValue_label.set_lcd_driver(_lcd);
    LSpeedValue_label.set_font_size(Aurora_Font::font_size_small);
    LSpeedValue_label.set_text("0.00");
    LSpeedValue_label.set_pos(42,125);
    LSpeedValue_label.show();

    RSpeedValue_label.set_font_driver(_font);
    RSpeedValue_label.set_lcd_driver(_lcd);
    RSpeedValue_label.set_font_size(Aurora_Font::font_size_small);
    RSpeedValue_label.set_text("0.00");
    RSpeedValue_label.set_pos(208,125);
    RSpeedValue_label.show();

    LPositionValue_label.set_font_driver(_font);
    LPositionValue_label.set_lcd_driver(_lcd);
    LPositionValue_label.set_font_size(Aurora_Font::font_size_small);
    LPositionValue_label.set_text("000");
    LPositionValue_label.set_pos(42,145);
    LPositionValue_label.show();

    RPositionValue_label.set_font_driver(_font);
    RPositionValue_label.set_lcd_driver(_lcd);
    RPositionValue_label.set_font_size(Aurora_Font::font_size_small);
    RPositionValue_label.set_text("000");
    RPositionValue_label.set_pos(208,145);
    RPositionValue_label.show();

    LDesireValue_label.set_font_driver(_font);
    LDesireValue_label.set_lcd_driver(_lcd);
    LDesireValue_label.set_font_size(Aurora_Font::font_size_small);
    LDesireValue_label.set_text("000");
    LDesireValue_label.set_pos(120,85);
    LDesireValue_label.set_color(0x00f8);
    LDesireValue_label.show();

    RDesireValue_label.set_font_driver(_font);
    RDesireValue_label.set_lcd_driver(_lcd);
    RDesireValue_label.set_font_size(Aurora_Font::font_size_small);
    RDesireValue_label.set_text("000");
    RDesireValue_label.set_pos(285,85);
    RDesireValue_label.set_color(0x00f8);
    RDesireValue_label.show();


    rt_thread_delay(30);
    ui2foc_msg.type = enable_detail;
    ui2foc_msg.cmd  = IS_CMD;
    rt_thread_delay(30);
}


void Aurora_Info::updateDetail(Aurora_FOC2UI_Detail detail){

    Lmode = detail.LMode;
    Rmode = detail.RMode;

    _lcd->fillpart(40,65,159,171,0xffff);
    _lcd->fillpart(205,65,319 ,171,0xffff);
    char data[10];
    snprintf(data,10,"%.2lf",detail.LUq);
    LUqValue_label.set_text(data);
    snprintf(data,10,"%.2lf",detail.LCurrent);
    LCurrentValue_label.set_text(data);
    snprintf(data,10,"%.2lf",detail.LSpeed);
    LSpeedValue_label.set_text(data);
    snprintf(data,10,"%.3d",(int)detail.LPosition);
    LPositionValue_label.set_text(data);
    snprintf(data,10,"%.2lf",detail.LDesire);
    LDesireValue_label.set_text(data);

    snprintf(data,10,"%.2lf",detail.RUq);
    RUqValue_label.set_text(data);
    snprintf(data,10,"%.2lf",detail.RCurrent);
    RCurrentValue_label.set_text(data);
    snprintf(data,10,"%.2lf",detail.RSpeed);
    RSpeedValue_label.set_text(data);
    snprintf(data,10,"%.3d",(int)detail.RPosition);
    RPositionValue_label.set_text(data);
    snprintf(data,10,"%.2lf",detail.RDesire);
    RDesireValue_label.set_text(data);

    if( Lmode == 0 || Lmode == 1){
        LArrow_label.set_pos(95,85);
        LDesireValue_label.set_pos(112,85);
    }
    else if(Lmode == 2){
        LArrow_label.set_pos(95,105);
        LDesireValue_label.set_pos(112,105);
    }
    else if(Lmode == 3){
        LArrow_label.set_pos(95,125);
        LDesireValue_label.set_pos(112,125);
    }
    else if(Lmode == 4){
        LArrow_label.set_pos(95,145);
        LDesireValue_label.set_pos(112,145);
    }
    else
        ;
    if( Rmode == 0 || Rmode == 1){
        RArrow_label.set_pos(256,85);
        RDesireValue_label.set_pos(272,85);
    }
    else if( Rmode == 2){
        RArrow_label.set_pos(256,105);
        RDesireValue_label.set_pos(272,105);
    }
    else if( Rmode == 3){
        RArrow_label.set_pos(256,125);
        RDesireValue_label.set_pos(272,125);
    }
    else if( Rmode == 4){
        RArrow_label.set_pos(256,145);
        RDesireValue_label.set_pos(272,145);
    }



    LUqValue_label.show();
    LCurrentValue_label.show();
    LSpeedValue_label.show();
    LPositionValue_label.show();
    LArrow_label.show();
    LDesireValue_label.show();

    RUqValue_label.show();
    RCurrentValue_label.show();
    RSpeedValue_label.show();
    RPositionValue_label.show();
    RArrow_label.show();
    RDesireValue_label.show();
}

void Aurora_Info::updateMode(uint8_t L,uint8_t R){
    if( L == 0){
        LmodeValue_label.set_text("暂    停");
    }
    else if (L == 1){
        LmodeValue_label.set_text("开    环");
    }
    else if (L == 2){
        LmodeValue_label.set_text("电    流");
    }
    else if (L == 3){
        LmodeValue_label.set_text("速    度");
    }
    else if (L == 4){
        LmodeValue_label.set_text("位    置");
    }
    LmodeValue_label.show();

    if( R == 0){
        RmodeValue_label.set_text("暂    停");
    }
    else if (R == 1){
        RmodeValue_label.set_text("开    环");
    }
    else if (R == 2){
        RmodeValue_label.set_text("电    流");
    }
    else if (R == 3){
        RmodeValue_label.set_text("速    度");
    }
    else if (R == 4){
        RmodeValue_label.set_text("位    置");
    }
    RmodeValue_label.show();
}